package com.itheima.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.aliyun.tea.*;
/**
 * 发送手机验证码工具类，使用阿里云短信服务
 *
 * @author 51585
 */
@Component
public class SmsUtil {

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送手机号登录的验证码
     * @param code
     * @param phone
     * @return
     * @throws Exception
     */
    public String sendPhoneCode(String code, String phone) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = SmsUtil.createClient("LTAI5t93DQZVq5Ca1P8PLf71", "g2Bl0DHAc8D0UowTLzsntDQl42kI2d");
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        SendSmsResponse sendSmsResponse =new SendSmsResponse();
        try {
            //短信签名名称
            sendSmsRequest.setSignName("叶世铭的毕设手机验证码")
                    //短信模板
                    .setTemplateCode("SMS_263495085")
                    //接受短信号码
                    .setPhoneNumbers(phone)
                    //短信内容
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            // 复制代码运行请自行打印 API 的返回值
            sendSmsResponse=client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            return "短信发送失败";
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return "短信发送失败";
        }
        return sendSmsResponse.body.message;

//        return "OK";
    }

    /**
     * 发送未读通知给对应用户
     * @param phone
     * @return
     * @throws Exception
     */
    public String sendNotice(String phone) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = SmsUtil.createClient(accessKeyId, accessKeySecret);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        try {
            //短信签名名称
            sendSmsRequest.setSignName("叶世铭的毕设通知")
                    //短信模板
                    .setTemplateCode("SMS_263560087")
                    //接受短信号码
                    .setPhoneNumbers(phone);
            // 复制代码运行请自行打印 API 的返回值
            sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            return "短信发送失败";
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return "短信发送失败";
        }
        return sendSmsResponse.body.message;
    }

}
